<template>
    <div style="">
        <div class="container" v-show="!showEdit">
            <div class="handle-box">
                <!-- <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button> -->
                <el-input v-model="loginNo" placeholder="账号" class="handle-input mr10"></el-input>
                <el-input v-model="userName" placeholder="姓名" class="handle-input mr10"></el-input>
                <el-input v-model="mobile" placeholder="手机号" class="handle-input mr10"></el-input>
                <el-select v-model="roleName" filterable placeholder="请选择" class="handle-select mr10" @visible-change="getRoles">
                    <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.name"> </el-option>
                </el-select>
                <el-select v-model="status" placeholder="状态" class="handle-select mr10">
                    <el-option key="0" label="启用" value="0"></el-option>
                    <el-option key="-1" label="禁用" value="-1"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
            </div>
            <div
                style=" display: flex;
                justify-content: flex-end;
                margin-bottom: 20px;"
            >
                <el-button type="primary" @click="addAccount">新建账号</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column type="index" :index="methodIndex" label="序号" width="55" align="center"></el-table-column>
                <el-table-column prop="loginNo" label="账号"></el-table-column>
                <el-table-column prop="userName" label="姓名"></el-table-column>
                <el-table-column prop="deptName" label="部门"></el-table-column>
                <el-table-column prop="jobPosition" label="职位"></el-table-column>
                <el-table-column prop="mobile" label="手机号"></el-table-column>
                <el-table-column label="角色" width="120">
                    <template slot-scope="scope">
                        <el-tag style="margin-right: 5px;margin-top: 5px;" v-for="(role, idx) in scope.row.roles" :key="idx">{{
                            role.name
                        }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="remarks" label="备注"></el-table-column>
                <el-table-column label="创建时间" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.createTime | dateFilter }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态" width="80" align="center">
                    <template slot-scope="scope" v-if="scope.row.loginNo != 'admin'">
                        <el-button type="text" :class="{ red: scope.row.status == -1 }" @click="handleStatus(scope.$index, scope.row)">{{
                            scope.row.status == -1 ? '禁用' : '启用'
                        }}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" v-if="scope.row.loginNo != 'admin'" @click="handlePassword(scope.$index, scope.row)"
                            >重置密码</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next,jumper"
                    :current-page="pageNo"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <edit-page v-if="showEdit" :isAdd="isAdd" :selRole="selRole" :selItem="editItem" @back="back" @save="save"></edit-page>

        <!-- 状态弹出框 -->
        <el-dialog class="popView" :visible.sync="editStatus" v-if="editStatus" :show-close="false" width="200px">
            <div class="main">
                <span class="title">请确认是否{{ selItem.status == -1 ? '启用' : '禁用' }}</span>
                <div class="btns">
                    <el-button @click="editStatus = false">取 消</el-button>
                    <el-button type="primary" @click="saveStatus">确认</el-button>
                </div>
            </div>
        </el-dialog>
        <!-- 重置密码弹出框 -->
        <el-dialog class="popView" :visible.sync="resetPassword" v-if="resetPassword" :show-close="false" width="200px">
            <div class="main">
                <span class="title">请确认是否重置密码</span>
                <div class="btns">
                    <el-button @click="resetPassword = false">取 消</el-button>
                    <el-button type="primary" @click="reSet">确认</el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import editPage from '../common/editPage';
import { fetchData } from '../../api/index';
import EditPage from '../common/editPage.vue';
export default {
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            delList: [],

            loginNo: null,
            userName: null,
            mobile: null,
            email: null,
            roleName: null,
            status: null,
            pageNo: 1,
            pageSize: 10,
            editStatus: false,
            selItem: null,
            pageTotal: 0,
            resetPassword: false,
            showEdit: false,
            editItem: null,
            isAdd: false,
            selRole: [],
            roles: []
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$api
                .adminUserPageQuery({
                    pageNo: this.pageNo,
                    pageSize: this.pageSize,
                    param: {
                        loginNo: this.loginNo,
                        userName: this.userName,
                        mobile: this.mobile,
                        roleName: this.roleName,
                        status: this.status
                    }
                })
                .then(res => {
                    if (res.code && res.code == '1001') {
                    } else {
                        this.tableData = res.data.result || [];
                        this.pageTotal = res.data.total;
                    }
                });
        },
        // changeRole(e) {
        //     console.log('🚀 ~ file: PortalSystemAccount.vue ~ line 175 ~ changeRole ~ e', e);
        // },
        getRoles(e) {
            if (!e) {
                return;
            }
            this.$api
                .get({
                    url: '/adminUser/getCanAllocatedRole'
                })
                .then(res => {
                    this.roles = res.data || [];
                })
                .catch(err => {});
        },
        // 触发搜索按钮
        handleSearch() {
            this.pageNo = 1;
            this.getData();
        },
        // 重置密码
        handlePassword(index, item) {
            this.selItem = item;
            this.resetPassword = true;
        },
        reSet() {
            this.$api
                .adminUserResetPw({
                    loginNo: this.selItem.loginNo
                })
                .then(res => {
                    this.resetPassword = false;
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 设置状态
        handleStatus(index, item) {
            this.selItem = item;
            this.editStatus = true;
        },
        saveStatus() {
            this.$api
                .changeStatus({
                    userId: this.selItem.id,
                    status: this.selItem.status == -1 ? 0 : -1
                })
                .then(res => {
                    this.editStatus = false;
                    this.getData();
                });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.showEdit = true;
            this.editItem = JSON.parse(JSON.stringify(row));
            this.isAdd = false;
            this.selRole = (this.editItem.roles || []).map(x => x.id);
        },
        // 新增
        addAccount() {
            this.showEdit = true;
            this.isAdd = true;
            this.editItem = {
                loginNo: '',
                userName: '',
                email: '',
                roleName: '',
                status: '',
                deptName: '',
                jobPosition: '',
                remarks: ''
            };
            this.selRole = [];
        },
        back() {
            this.selItem = null;
            this.showEdit = false;
        },
        // 保存
        save() {
            this.selItem = null;
            this.showEdit = false;
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.pageNo = val;
            this.getData();
        },
        methodIndex(index) {
            return index + 1;
        }
    },
    components: {
        EditPage
    }
};
</script>

<style lang="scss" scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 100px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
/deep/.popView {
    display: flex;
    align-items: center;
    justify-content: center;
    .el-dialog__header {
        padding: 0;
    }
    .el-dialog__body {
        padding: 0;
    }
    .main {
        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: #fff;
        border-radius: 5px;
        padding: 20px 0;
        .title {
            font-size: 17px;
            font-weight: bold;
            margin-bottom: 20px;
        }
        .btns {
            width: 100%;
            display: flex;
            justify-content: space-around;
        }
    }
}
</style>
